//
//  YKTabBarController.m
//  youkan
//
//  Created by mac mini on 16/2/24.
//  Copyright © 2016年 Honsintech. All rights reserved.
//

#import "YKTabBarController.h"
#import "UIImage+image.h"
#import "NSString+YKWeiBo.h"
/** 自定义带有气泡效果的TabBar */
#import "YKTabBar.h"
#import "GFTabBar.h"
/** 自定义导航栏控制器 */
#import "YKNavigationController.h"
/** 首页控制器 */
#import "YKHomeViewController.h"
/** 消息控制器 */
#import "YKMessageViewController.h"
/** 发现控制器 */
#import "YKDiscoverViewController.h"
/** 我的控制器 */
#import "YKProfileViewController.h"
/** 发表内容控制器 */
#import "YKCompseViewController.h"
/** 用户工具（获取用户相关信息） */
#import "YKUserTool.h"
/** 用户状态模型 */
#import "YKUserResult.h"
@interface YKTabBarController ()<YKTabBarDelegate>
@property(nonatomic,strong)NSMutableArray *items;
@property(nonatomic,weak)YKHomeViewController *home;
@property(nonatomic,weak)YKMessageViewController *message;
@property(nonatomic,weak)YKProfileViewController *profile;

@end

@implementation YKTabBarController
// 什么时候调用：程序一启动的时候就会把所有的类加载进内存
//!> 作用：加载类的时候调用(每次加载都会调用)
//+ (void)load
//{
//}
// 什么调用：当第一次使用这个类或者子类的时候调用
// 作用：初始化类
// appearance只要一个类遵守UIAppearance，就能获取全局的外观，UIView
//!>  在程序运行期间只会调用一次
//+(void)initialize{
//    // 获取所有的tabBarItem外观标识
    //    UITabBarItem *item = [UITabBarItem appearance];
//
//    // self -> CZTabBarController
//    // 获取当前这个类下面的所有tabBarItem，设置标题为橙色（使用可以变字体）
//    UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];
//    NSMutableDictionary *att =[NSMutableDictionary dictionary];
//    att[NSForegroundColorAttributeName]=[UIColor orangeColor];
//    [item setTitleTextAttributes:att forState:UIControlStateSelected];
//}
-(NSMutableArray *)items{
    if (!_items) {
        _items =[NSMutableArray array];
    }
    return _items;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    /*
////    UIViewController中的view是懒加载
////    但 UITabBarController在一创建的时候就加载
//    [self setUpAllChildViewController];
//    NSLog(@"subview1%@",self.tabBar);
//    YKTabBar *tabBar=[[YKTabBar alloc]initWithFrame:self.tabBar.frame];
////    self.tabBar =tabBar; 利用KVC赋值解决readonly
//    [self setValue:tabBar forKeyPath:@"tabBar"];
//    NSLog(@"subview2%@",self.tabBar);
     */
    [self setUpAllChildViewController];
    [self setUpTabBar];
//    请求微博的未读数 (每隔一段时间10s请求一次)
    [NSTimer scheduledTimerWithTimeInterval:10
                                     target:self
                                   selector:@selector(requestUnreda)
                                   userInfo:nil
                                    repeats:YES];

}
-(void)requestUnreda{
   
    [YKUserTool unreadWithSucces:^(YKUserResult *result) {
        //设置 首页，消息，我的 未读数
        self.home.tabBarItem.badgeValue = [NSString stringWithBadgeValue:result.status];
        self.message.tabBarItem.badgeValue = [NSString stringWithBadgeValue:result.messageCount];
        self.profile.tabBarItem.badgeValue = [NSString stringWithBadgeValue:result.follower];
        //        所有的未读数
        [UIApplication sharedApplication].applicationIconBadgeNumber =result.totoalCount;
    } failure:^(NSError *error) {
        
    }];

}
-(void)setUpTabBar{
    /**
    YKTabBar *tabBar =[[YKTabBar alloc]initWithFrame:self.tabBar.frame];
    tabBar.backgroundColor =[UIColor whiteColor];
    tabBar.delegate = self;
    tabBar.items = self.items;
//    YKLog(@"tabbar==%@",tabBar);
//!>    将自定义TabBar添加进去，并移除原来的
    [self.view addSubview: tabBar];
    [self.tabBar removeFromSuperview];*/
    GFTabBar *tabBar = [[GFTabBar alloc]init];
    [tabBar.plusButton addTarget:self action:@selector(tabBarDidClickPlusButton:) forControlEvents:UIControlEventTouchUpInside];

    [self setValue:tabBar forKey:@"tabBar"];

}
#pragma mark YKTabBarDelegate 的方法
-(void)tabBar:(YKTabBar *)tabBar didClickButton:(NSInteger)index{
    if (index==0 && self.selectedIndex ==index) {
        [_home refresh];
    }
    self.selectedIndex =index;

}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    NSLog(@"subview3%@",self.tabBar.subviews);


}
-(void)setUpAllChildViewController{
    YKHomeViewController *home =[[YKHomeViewController alloc]init];
    [self setUpOneChildViewController:home image:[UIImage imageWithOriginalName:@"tabbar_home"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_home_selected"] title:@"首页"];
    _home =home;
    YKMessageViewController *message =[[YKMessageViewController alloc]init];
    message.view.backgroundColor =[UIColor lightGrayColor];
    [self setUpOneChildViewController:message image:[UIImage imageWithOriginalName:@"tabbar_message_center"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_message_center_selected"] title:@"消息"];
    _message=message;
    YKDiscoverViewController *discover =[[YKDiscoverViewController alloc]init];
    [self setUpOneChildViewController:discover image:[UIImage imageWithOriginalName:@"tabbar_discover"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_discover_selected"] title:@"发现"];

    YKProfileViewController *profile =[[YKProfileViewController alloc]init];
    profile.view.backgroundColor =[UIColor purpleColor];
    [self setUpOneChildViewController:profile image:[UIImage imageWithOriginalName:@"tabbar_profile"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_profile_selected"] title:@"我"];
    _profile = profile;
}
-(void)setUpOneChildViewController:(UIViewController*)vc image:(UIImage*)image selectedImage:(UIImage*)seleImage title:(NSString*)title{
    vc.title =title;
    vc.tabBarItem.image=image;
//    vc.tabBarItem.badgeValue =@"5";
    vc.tabBarItem.selectedImage =seleImage;
//    保存tabBarItem 模型到数组
    [self.items addObject:vc.tabBarItem];

    YKNavigationController *nav =[[YKNavigationController alloc]initWithRootViewController:vc];
    [self addChildViewController:nav];
}
//!> 发表微博的响应方法

-(void)tabBarDidClickPlusButton:(GFTabBar *)plusButton{
    YKCompseViewController *compseVC=[[YKCompseViewController alloc]init];
    YKNavigationController *nav =[[YKNavigationController alloc]initWithRootViewController:compseVC];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
